﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GS.Cricket.Web.mapclass;

namespace GS.Cricket.Web
{
    public partial class PointsTable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadPointsTable();
        }


        private void LoadPointsTable()
        {
            var matchTeams = new GS.Cricket.Service.Impl.MatchTeamService().GetAll().ToList();
            var matches = new GS.Cricket.Service.Impl.MatchService().GetAllMatches().ToList();
            var teams = new GS.Cricket.Service.Impl.TeamService().GetAll().ToList();
            var groups = new GS.Cricket.Service.Impl.GroupService().GetAll().ToList();
            var groupA = groups[0];
            var groupB = groups[1];

            Label_GroupA.Text = groupA.Name;
            Label_GroupB.Text = groupB.Name;

            var GroupA = GetGroupWiseTable(groupA, teams, matchTeams);
            var GroupB = GetGroupWiseTable(groupB, teams, matchTeams);

            // Sort results
            GroupA = GroupA.OrderByDescending(gt => gt.Points).ThenByDescending(gt => gt.RunRate).ToList();
            GroupB = GroupB.OrderByDescending(gt => gt.Points).ThenByDescending(gt => gt.RunRate).ToList();

            BindPointsGrid(GroupAGrid, GroupA);
            BindPointsGrid(GroupBGrid, GroupB);

        }

        private void BindPointsGrid(GridView grid, List<TeamPoints> group)
        {
            grid.DataSource = group;
            grid.AutoGenerateColumns = false;
            grid.DataBind();
        }


        private List<TeamPoints> GetGroupWiseTable(Entities.Group group, List<Entities.Team> teams, List<Entities.MatchTeam> matchTeams)
        {
            List<TeamPoints> Group = new List<TeamPoints>();
            var GroupATeams = teams.Where(t => t.Group == group.Id).ToList();
            foreach (var team in GroupATeams)
            {
                var mts = matchTeams.Where(mt => mt.TeamId == team.Id).ToList();
                var point = 0;
                var runsScored = 0;
                var runsConceeded = 0;
                var oversPlayed = 0.0;
                var oversBowled = 0.0;
                var matchPlayed = mts.Count();
                var won = mts.Where(m => m.HasWon).Count();
                var lost = mts.Where(m => !m.HasWon).Count();
                foreach (var mt in mts)
                {
                    if (mt.HasWon)
                    {
                        point += 2;
                    }
                    runsScored += mt.RunsScored;
                    runsConceeded += mt.RunsConceeded;
                    oversBowled += mt.OversBowled;
                    oversPlayed += mt.OversPlayed;
                }

                // Net run rate
                var nrr = ((Double)runsScored / oversPlayed) - ((Double)runsConceeded / oversBowled);
                nrr = Double.IsNaN(nrr) ? 0.0 : nrr;

                TeamPoints teamPoint = new TeamPoints();
                teamPoint.GroupId = group.Id;
                teamPoint.Points = point;
                teamPoint.TeamName = team.Name;
                teamPoint.RunRate = Math.Round(nrr, 3);
                teamPoint.Matchplayed = matchPlayed;
                teamPoint.Won = won;
                teamPoint.Lost = lost;

                Group.Add(teamPoint);
            }
            return Group;
        }

    }
}